<?php
namespace Rainrock\Framework\Controller;


use Rainrock\Framework\kernel\core\Controller;
use Rainrock\Framework\kernel\base\Base;
use Rainrock\Framework\kernel\base\Request;


/**
*	快捷登录
*/
class OpenssoController extends Controller{
	
	public function indexAction()
	{
		$checkurl 	= Base::getConfig('sso_checkurl'); //验证地址
		$ssokey 	= Base::getConfig('sso_key'); //验证key
		if(!$checkurl)return MsgView::error('未配置验证地址');
		
		$ssotoken	= Request::get('ssotoken');
		$device		= Request::get('device');
		$cfrom		= Request::get('cfrom','sso');
		$lurl		= urldecode(Request::get('backurl')); //登录成功跳转地址urlencode
		
		if($this->isempt($ssotoken))return MsgView::error('没有参数ssotoken');
		
		$jg 	= $this->contain($checkurl,'?')?'&':'?';
		
		$url 	= $checkurl.$jg.'ssotoken='.$ssotoken.'&ssokey='.$ssokey.'&device='.$device.'&cfrom='.$cfrom.'&backtype=json';
		$result = $this->c('curl')->getcurl($url); //要返回用户帐号/手机号
		if($this->isempt($result))return MsgView::error('验证失败没有返回值');
		
		$arr	= json_decode($result, true);
		if(!isset($arr['user']))return MsgView::error('没有返回用户名');
		$mobile = $this->arrvalue($arr, 'mobile');
		$name   = $this->arrvalue($arr, 'name');
		$lmsgs  = 'SSO快捷登录';
		$obj 	= $this->m('systoken');
		$pass 	= '123';//随机就可以
		
		$barr	= $obj->login($arr['user'], $pass, $cfrom, '', $device, '', $lmsgs);
		if($mobile && !$barr['success'])$barr = $obj->login($mobile, $pass,$cfrom, '', $device, '', $lmsgs);
		if($name && !$barr['success'])$barr = $obj->login($name, $pass,$cfrom, '', $device, '', $lmsgs);
		
		if(!$barr['success'])return MsgView::error($barr['msg']);
		
		$adminurl = Base::getConfig('adminurl');
		if($lurl)$adminurl = $lurl;
		$this->location($adminurl);
	}
	
}